package org.example.platform.elasticsearch.controller;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.search.SearchException;
import org.example.platform.elasticsearch.entity.Hotel;
import org.example.platform.elasticsearch.entity.HotelDocRequest;
import org.example.platform.elasticsearch.service.ESQueryCommonService;
import org.example.platform.elasticsearch.service.ESQueryDocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class ESQueryController {
    @Autowired
    ESQueryDocService eSQueryDocService;
    @Autowired
    ESQueryCommonService eSQueryCommonService;


    /**
     * 计数查
     *
     * @param hotelDocRequest
     * @return
     */
    @PostMapping("/query/count")
    public Long queryCount(@RequestBody HotelDocRequest hotelDocRequest) {

        try {
            Long resultMap = eSQueryDocService.getCityCount(hotelDocRequest);
            return resultMap;
        } catch (SearchException e) {
            log.warn("搜索发生异常，原因为:{}", e.getMessage());
            throw new RuntimeException("搜索发生异常，原因为:{}" + e.getMessage());
        } catch (Exception e) {
            log.error("服务发生异常，原因为:{}", e.getMessage());
            throw new RuntimeException("服务发生异常，原因为:{}" + e.getMessage());
        }
    }

    /**
     * 指定返回字段
     *
     * @param hotelDocRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/query/source")
    public List<Hotel> queryBySource(@RequestBody HotelDocRequest hotelDocRequest) throws Exception {
        try {
            List<Hotel> resultMap = eSQueryDocService.queryBySource(hotelDocRequest);
            return resultMap;
        } catch (SearchException e) {
            log.warn("搜索发生异常，原因为:{}", e.getMessage());
            throw new RuntimeException("搜索发生异常，原因为:{}" + e.getMessage());
        } catch (Exception e) {
            log.error("服务发生异常，原因为:{}", e.getMessage());
            throw new RuntimeException("服务发生异常，原因为:{}" + e.getMessage());
        }
    }

    /**
     * 分页查
     *
     * @param hotelDocRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/queryLimit")
    public List<Hotel> queryLimit(@RequestBody HotelDocRequest hotelDocRequest) throws Exception {
        try {
            List<Hotel> resultMap = eSQueryDocService.queryLimit(hotelDocRequest);
            return resultMap;
        } catch (SearchException e) {
            log.warn("搜索发生异常，原因为:{}", e.getMessage());
            throw new RuntimeException("搜索发生异常，原因为:{}" + e.getMessage());
        } catch (Exception e) {
            log.error("服务发生异常，原因为:{}", e.getMessage());
            throw new RuntimeException("服务发生异常，原因为:{}" + e.getMessage());
        }
    }

    /**
     * mustNot、should、must、exist、range、filter、terms查询、日期类型的term查询、查询所有文档
     *
     * @param hotelDocRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/query")
    public List<Hotel> query(@RequestBody HotelDocRequest hotelDocRequest) throws Exception {
        try {
            List<Hotel> resultMap = eSQueryCommonService.query(hotelDocRequest);
            return resultMap;
        } catch (SearchException e) {
            log.warn("搜索发生异常，原因为:{}", e.getMessage());
            throw new Exception("搜索发生异常，原因为:{}" + e.getMessage());
        } catch (Exception e) {
            log.error("服务发生异常，原因为:{}", e.getMessage());
            throw new Exception("服务发生异常，原因为:{}" + e.getMessage());
        }
    }


}
